{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pyalink.alink import *\n",
    "useLocalEnv(1)\n",
    "\n",
    "from utils import *\n",
    "import os\n",
    "import pandas as pd\n",
    "\n",
    "pd.set_option('display.max_colwidth', 1000)\n",
    "\n",
    "DATA_DIR = ROOT_DIR + \"iris\" + os.sep\n",
    "\n",
    "ORIGIN_FILE = \"iris.data\";\n",
    "\n",
    "TRAIN_FILE = \"train.ak\";\n",
    "TEST_FILE = \"test.ak\";\n",
    "\n",
    "SCHEMA_STRING = \"sepal_length double, sepal_width double, petal_length double, petal_width double, category string\"\n",
    "\n",
    "FEATURE_COL_NAMES = [\"sepal_length\", \"sepal_width\", \"petal_length\", \"petal_width\"]\n",
    "\n",
    "LABEL_COL_NAME = \"category\";\n",
    "\n",
    "PREDICTION_COL_NAME = \"pred\";\n",
    "PRED_DETAIL_COL_NAME = \"pred_info\";\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#c_1\n",
    "source = CsvSourceBatchOp()\\\n",
    "    .setFilePath(DATA_DIR + ORIGIN_FILE)\\\n",
    "    .setSchemaStr(SCHEMA_STRING);\n",
    "\n",
    "source\\\n",
    "    .lazyPrint(5, \"origin file\")\\\n",
    "    .lazyPrintStatistics(\"stat of origin file\")\\\n",
    "    .link(\n",
    "        CorrelationBatchOp()\\\n",
    "            .setSelectedCols(FEATURE_COL_NAMES)\\\n",
    "            .lazyPrintCorrelation()\n",
    "    );\n",
    "\n",
    "source.groupBy(LABEL_COL_NAME, LABEL_COL_NAME + \", COUNT(*) AS cnt\").lazyPrint(-1);\n",
    "\n",
    "BatchOperator.execute();\n",
    "\n",
    "splitTrainTestIfNotExist(source, DATA_DIR + TRAIN_FILE, DATA_DIR + TEST_FILE, 0.9);\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#c_2\n",
    "train_data = AkSourceBatchOp().setFilePath(DATA_DIR + TRAIN_FILE);\n",
    "test_data = AkSourceBatchOp().setFilePath(DATA_DIR + TEST_FILE);\n",
    "\n",
    "trainer = NaiveBayesTrainBatchOp()\\\n",
    "    .setFeatureCols(FEATURE_COL_NAMES)\\\n",
    "    .setLabelCol(LABEL_COL_NAME);\n",
    "\n",
    "predictor = NaiveBayesPredictBatchOp()\\\n",
    "    .setPredictionCol(PREDICTION_COL_NAME)\\\n",
    "    .setPredictionDetailCol(PRED_DETAIL_COL_NAME);\n",
    "\n",
    "train_data.link(trainer);\n",
    "\n",
    "predictor.linkFrom(trainer, test_data);\n",
    "\n",
    "trainer.lazyPrintModelInfo();\n",
    "\n",
    "predictor.lazyPrint(1, \"< Prediction >\");\n",
    "\n",
    "predictor\\\n",
    "    .link(\n",
    "        EvalMultiClassBatchOp()\\\n",
    "            .setLabelCol(LABEL_COL_NAME)\\\n",
    "            .setPredictionCol(PREDICTION_COL_NAME)\\\n",
    "            .setPredictionDetailCol(PRED_DETAIL_COL_NAME)\\\n",
    "            .lazyPrintMetrics(\"NaiveBayes\")\n",
    "    );\n",
    "\n",
    "BatchOperator.execute();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#c_3\n",
    "train_data = AkSourceBatchOp().setFilePath(DATA_DIR + TRAIN_FILE);\n",
    "test_data = AkSourceBatchOp().setFilePath(DATA_DIR + TEST_FILE);\n",
    "\n",
    "OneVsRest()\\\n",
    "    .setClassifier(\n",
    "        LogisticRegression()\\\n",
    "            .setFeatureCols(FEATURE_COL_NAMES)\\\n",
    "            .setLabelCol(LABEL_COL_NAME)\\\n",
    "            .setPredictionCol(PREDICTION_COL_NAME)\\\n",
    "    )\\\n",
    "    .setNumClass(3)\\\n",
    "    .fit(train_data)\\\n",
    "    .transform(test_data)\\\n",
    "    .link(\n",
    "        EvalMultiClassBatchOp()\\\n",
    "            .setLabelCol(LABEL_COL_NAME)\\\n",
    "            .setPredictionCol(PREDICTION_COL_NAME)\\\n",
    "            .lazyPrintMetrics(\"OneVsRest_LogisticRegression\")\n",
    "    );\n",
    "\n",
    "OneVsRest()\\\n",
    "    .setClassifier(\n",
    "        GbdtClassifier()\\\n",
    "            .setFeatureCols(FEATURE_COL_NAMES)\\\n",
    "            .setLabelCol(LABEL_COL_NAME)\n",
    "            .setPredictionCol(PREDICTION_COL_NAME)\\\n",
    "    )\\\n",
    "    .setNumClass(3)\\\n",
    "    .fit(train_data)\\\n",
    "    .transform(test_data)\\\n",
    "    .link(\n",
    "        EvalMultiClassBatchOp()\\\n",
    "            .setLabelCol(LABEL_COL_NAME)\\\n",
    "            .setPredictionCol(PREDICTION_COL_NAME)\\\n",
    "            .lazyPrintMetrics(\"OneVsRest_GBDT\")\n",
    "    );\n",
    "\n",
    "OneVsRest()\\\n",
    "    .setClassifier(\n",
    "        LinearSvm()\\\n",
    "            .setFeatureCols(FEATURE_COL_NAMES)\\\n",
    "            .setLabelCol(LABEL_COL_NAME)\n",
    "            .setPredictionCol(PREDICTION_COL_NAME)\\\n",
    "    )\\\n",
    "    .setNumClass(3)\\\n",
    "    .fit(train_data)\\\n",
    "    .transform(test_data)\\\n",
    "    .link(\n",
    "        EvalMultiClassBatchOp()\\\n",
    "            .setLabelCol(LABEL_COL_NAME)\\\n",
    "            .setPredictionCol(PREDICTION_COL_NAME)\\\n",
    "            .lazyPrintMetrics(\"OneVsRest_LinearSvm\")\n",
    "    );\n",
    "\n",
    "BatchOperator.execute()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#c_4\n",
    "train_data = AkSourceBatchOp().setFilePath(DATA_DIR + TRAIN_FILE);\n",
    "test_data = AkSourceBatchOp().setFilePath(DATA_DIR + TEST_FILE);\n",
    "\n",
    "Softmax()\\\n",
    "    .setFeatureCols(FEATURE_COL_NAMES)\\\n",
    "    .setLabelCol(LABEL_COL_NAME)\\\n",
    "    .setPredictionCol(PREDICTION_COL_NAME)\\\n",
    "    .enableLazyPrintTrainInfo()\\\n",
    "    .enableLazyPrintModelInfo()\\\n",
    "    .fit(train_data)\\\n",
    "    .transform(test_data)\\\n",
    "    .link(\n",
    "        EvalMultiClassBatchOp()\\\n",
    "            .setLabelCol(LABEL_COL_NAME)\\\n",
    "            .setPredictionCol(PREDICTION_COL_NAME)\\\n",
    "            .lazyPrintMetrics(\"Softmax\")\n",
    "    );\n",
    "\n",
    "BatchOperator.execute();\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#c_5\n",
    "train_data = AkSourceBatchOp().setFilePath(DATA_DIR + TRAIN_FILE);\n",
    "test_data = AkSourceBatchOp().setFilePath(DATA_DIR + TEST_FILE);\n",
    "\n",
    "MultilayerPerceptronClassifier()\\\n",
    "    .setLayers([4, 12, 3])\\\n",
    "    .setFeatureCols(FEATURE_COL_NAMES)\\\n",
    "    .setLabelCol(LABEL_COL_NAME)\\\n",
    "    .setPredictionCol(PREDICTION_COL_NAME)\\\n",
    "    .fit(train_data)\\\n",
    "    .transform(test_data)\\\n",
    "    .link(\n",
    "        EvalMultiClassBatchOp()\\\n",
    "            .setLabelCol(LABEL_COL_NAME)\\\n",
    "            .setPredictionCol(PREDICTION_COL_NAME)\\\n",
    "            .lazyPrintMetrics(\"MultilayerPerceptronClassifier [4, 12, 3]\")\n",
    "    );\n",
    "\n",
    "MultilayerPerceptronClassifier()\\\n",
    "    .setLayers([4, 3])\\\n",
    "    .setFeatureCols(FEATURE_COL_NAMES)\\\n",
    "    .setLabelCol(LABEL_COL_NAME)\\\n",
    "    .setPredictionCol(PREDICTION_COL_NAME)\\\n",
    "    .fit(train_data)\\\n",
    "    .transform(test_data)\\\n",
    "    .link(\n",
    "        EvalMultiClassBatchOp()\\\n",
    "            .setLabelCol(LABEL_COL_NAME)\\\n",
    "            .setPredictionCol(PREDICTION_COL_NAME)\\\n",
    "            .lazyPrintMetrics(\"MultilayerPerceptronClassifier [4, 3]\")\n",
    "    );\n",
    "\n",
    "BatchOperator.execute();\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
